﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AjaxControlToolkit;
using System.Web.UI;
using System.Globalization;

namespace PortalCurriculo.Util.CustomControls
{
    public class MaskedEdit : MaskedEditExtender
    {
        public new enum MaskedEditType
        {
            None,
            Date,
            Number,
            Time,
            DateTime,
            SimplePhone,
            FullPhone,
            PhoneDDD,
            CPF,
            CNPJ,
            Birthday,
            RG,
            CEP
        }

        public MaskedEditType Type { get; set; }
        public CultureInfo Culture { get; set; }

        [Obsolete]
        private new AjaxControlToolkit.MaskedEditType MaskType { get; set; }
        [Obsolete]
        private new string CultureName { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            FillMask();
            FillBaseMask();

            base.OnLoad(e);
        }

        private void FillBaseMask()
        {
            base.MaskType = AjaxControlToolkit.MaskedEditType.None;

            switch (Type)
            {
                case MaskedEditType.Date:
                case MaskedEditType.Birthday:
                    base.MaskType = AjaxControlToolkit.MaskedEditType.Date;
                    break;
                case MaskedEditType.DateTime:
                    base.MaskType = AjaxControlToolkit.MaskedEditType.DateTime;
                    break;
                case MaskedEditType.Time:
                    base.MaskType = AjaxControlToolkit.MaskedEditType.Time;
                    break;
                case MaskedEditType.Number:
                    base.MaskType = AjaxControlToolkit.MaskedEditType.Number;
                    break;
            }

            try
            {
                base.CultureName = Culture.Name;
            }
            catch (Exception)
            {
                base.CultureName = null;
            }            
        }

        private void FillMask()
        {
            if (!string.IsNullOrEmpty(Mask)) return;

            switch (Type)
            {
                case MaskedEditType.CPF:
                    Mask = "999999999-99";
                    break;
                case MaskedEditType.CNPJ:
                    Mask = "99.999.999/9999-99";
                    break;
                case MaskedEditType.CEP:
                    Mask = "99999-999";
                    break;
                case MaskedEditType.Date:
                case MaskedEditType.Birthday:
                    Mask = "99/99/9999";
                    break;
                case MaskedEditType.FullPhone:
                    Mask = "99 (99) 9999-9999";
                    break;
                case MaskedEditType.PhoneDDD:
                    Mask = "(99) 9999-9999";
                    break;
                case MaskedEditType.SimplePhone:
                    Mask = "9999-9999";
                    break;
                case MaskedEditType.DateTime:
                    Mask = "99/99/9999 99:99";
                    break;
                case MaskedEditType.Time:
                    Mask = "99:99";
                    break;
                case MaskedEditType.Number:
                    Mask = "9,99";
                    break;
                case MaskedEditType.RG:
                    Mask = "99.999.999-?";
                    break;
            }
        }
    }
}
